<template>
	<view class="basic-info">
		<view class="c-row-box">
			<view class="c-row-head">
				<view class="c-row-title">订单信息</view>
			</view>
			<view class="c-row-item">
				<view class="c-row c-row-list">
					<view class="c-row-col c-row-col-list-item">
						<view class="col-title">
							<text style="color: red;">*</text>
							订单公司
						</view>
						<view class="col-text">
							<picker mode="selector" :range="companyList" range-key="title" @change="companyChange"
								:disabled="isPreview">
								<view>{{ selectedCompany }}</view>
							</picker>
						</view>
					</view>
				</view>

				<view class="c-row c-row-list">
					<view class="c-row-col c-row-col-list-item">
						<view class="col-title">
							<text style="color: red;">*</text>
							订单性质
						</view>
						<view class="col-text">
							<picker mode="selector" :range="orderNature" range-key="title" @change="orderNatureChange"
								:disabled="isPreview">
								<view>{{ dictionary.getTitle(orderNature, orderParams.order_nature, '请选择') }}</view>
							</picker>
						</view>
					</view>
				</view>

				<view class="c-row c-row-list" v-if="![0].includes(orderParams.order_nature)">
					<view class="c-row-col c-row-col-list-item">
						<view class="col-title">
							<text style="color: red;">*</text>
							订单类型
						</view>
						<view class="col-text" v-if="orderParams.order_nature==5">
							<picker mode="selector" :range="newsTypeList" range-key="title" @change="newsTypeChange"
								:disabled="isPreview">
								<view>{{ dictionary.getPickerTitle('newsTypeList', orderParams.news_type, '请选择') }}
								</view>
							</picker>
						</view>
						<view class="col-text" v-else>
							<picker mode="selector" :range="newsTypeListNew" range-key="title" @change="newsTypeChange"
								:disabled="isPreview">
								<view>{{ dictionary.getPickerTitle('newsTypeList', orderParams.news_type, '请选择') }}
								</view>
							</picker>
						</view>
					</view>
				</view>

				<!-- 婚庆信息1 婚车4 合作模式默认-->
				<view class="c-row c-row-list"
					v-if="orderParams.order_nature == 1 && !include(orderParams.news_type, ['', 1, 4])">
					<view class="c-row-col c-row-col-list-item">
						<view class="col-title">
							<text style="color: red;">*</text>
							合作模式
						</view>
						<view class="col-text">
							<picker mode="selector" :range="cooperationModeList" range-key="title"
								@change="cooperationModeChange" :disabled="isPreview">
								<view>{{ selectedCooperationMode }}</view>
							</picker>
						</view>
					</view>
				</view>

				<view class="c-row c-row-list" v-if="orderParams.order_nature == 1&& orderParams.news_type!=8">
					<view class="c-row-col c-row-col-list-item">
						<view class="col-title">
							<text style="color: red;">*</text>
							合同编号
						</view>
						<view class="col-text"><input type="text" @blur="checkContract"
								v-model="orderParams.contract_no" :disabled="isPreview" placeholder="请输入" /></view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import dictionary from '@/utils/dictionary.js';
	import request from '@/utils/request.js';
	import {
		include
	} from '@/utils/common.js';
	export default {
		props: {
			isPreview: {
				type: Boolean,
				default () {
					return false;
				}
			},
			order: {
				type: Object,
				default () {
					return {};
				}
			},
			orderParams: {
				type: Object,
				default () {
					return {};
				}
			},
			banquetParams: {
				type: Object,
				default () {
					return {};
				}
			},
			weddingParams: {
				type: Object,
				default () {
					return {};
				}
			},
			babyParams: {
				type: Object,
				default () {
					return {};
				}
			},
			meetingParams: {
				type: Object,
				default () {
					return {};
				}
			}
		},
		data() {
			return {
				// 订单性质
				orderNature: [{
						id: 1,
						type: 'order',
						title: '订单'
					},
					{
						id: 2,
						type: 'order',
						title: '返佣单'
					},
					{
						id: 3,
						type: 'earnest',
						title: '定金'
					},
					{
						id: 4,
						type: 'prepay',
						title: '意向金'
					},
					{
						id: 5,
						type: 'other',
						title: '其他'
					}
				],
				cooperationModeList: [{
						id: '2',
						title: '代收代付'
					},
					{
						id: '3',
						title: '代收代付+返佣单'
					},
					{
						id: '4',
						title: '一单一议'
					}
				],
				newsTypeListNew: []
			};
		},
		computed: {
			companyList: function() {
				let companyList = dictionary.getPicker('companyList');
				companyList = companyList.filter(ele => ele.is_valid == 1);

				return companyList;
			},
			selectedCompany: function() {
				return dictionary.getPickerTitle('companyList', this.orderParams.company_id, '请选择');
			},
			newsTypeList: function() {
				let newsTypeList = dictionary.getPicker('newsTypeList');
				return newsTypeList;
			},
			selectedCooperationMode: function() {
				let title = '请选择';
				this.cooperationModeList.forEach(item => {
					if (item.id == this.orderParams.cooperation_mode) {
						title = item.title;
					}
				});
				return title;
			}
		},
		beforeMount() {
			this.dictionary = dictionary;
			this.include = include;
		},
		methods: {
			// 选择公司
			companyChange(e) {
				let index = e.target.value;
				this.orderParams.company_id = this.companyList[index].id;
				if (this.orderParams.news_type == '0') {
					// 同步婚宴承办公司
					this.banquetParams.company_id = this.orderParams.company_id;
				} else if (this.orderParams.news_type == '1') {
					// 同步婚庆承办公司
					this.weddingParams.company_id = this.orderParams.company_id;
				} else if (this.orderParams.news_type == '2') {
					// 同步婚宴、婚庆的承办公司
					this.banquetParams.company_id = this.orderParams.company_id;
					this.weddingParams.company_id = this.orderParams.company_id;
				}
				this.$emit('getConfirmSequence');
			},
			// 订单性质
			orderNatureChange(e) {
				// this.$emit('resetValue');
				let index = e.target.value;
				this.order.news_type = '';
				this.orderParams.news_type = '';
				this.orderParams.cooperation_mode = 0;
				if (index == 1) {
					this.order.order_nature = '返佣单';
					this.orderParams.order_nature = 2;
					//this.orderParams.news_type = '0'; 之前返佣订单默认婚宴信息订单性质
					this.orderParams.cooperation_mode = 1;
				} else {
					this.order.order_nature = this.orderNature[index].title;
					this.orderParams.order_nature = this.orderNature[index].id;
					this.$emit('resetSettlement');
				}
				if (this.orderNature[index].id != 5) {
					this.newsTypeListNew = this.newsTypeList.filter(item => item.id !== "11" && item.id !== "12");
				}

				if (this.orderParams.order_nature == 1 || this.orderParams.order_nature == 2 || this.orderParams
					.order_nature == 5) {
					this.orderParams.audit_type = 'order';
				} else if (this.orderParams.order_nature == 3) {
					this.orderParams.audit_type = 'earnest';
				} else if (this.orderParams.order_nature == 4) {
					this.orderParams.audit_type = 'prepay';
				}
				this.$emit('getConfirmSequence');
			},
			// 订单类型
			newsTypeChange(e) {
				this.$emit('resetValue');

				let index = e.target.value;
				this.orderParams.news_type = this.newsTypeList[index].id;
				if (this.orderParams.news_type == '0') {
					// 婚宴信息
					this.banquetParams.company_id = this.orderParams.company_id;
				} else if (this.orderParams.news_type == '1' && this.orderParams.order_nature == '1') {
					// 订单性质同时为1为订单
					this.order.mode = '请选择';
					this.orderParams.cooperation_mode = 0;
				} else if (this.orderParams.news_type == '2') {
					this.banquetParams.company_id = this.orderParams.company_id;
					this.weddingParams.company_id = this.orderParams.company_id;
				} else if (this.orderParams.news_type == '7') {
					this.babyParams.company_id = this.orderParams.company_id;
				} else if (this.orderParams.news_type == '8') {
					this.meetingParams.company_id = this.orderParams.company_id;
				}
				this.$emit('getConfirmSequence');
			},

			// 合作模式
			cooperationModeChange(e) {
				// 重置返佣的结算信息
				this.$emit('resetSettlement');

				let index = e.target.value;
				this.orderParams.cooperation_mode = this.cooperationModeList[index].id;
				this.$emit('getConfirmSequence');
			},

			// 检查合同编号
			checkContract(e) {

				let contract_no = e.detail.value;
				const item = this.companyList.find(obj => obj.id === this.orderParams.company_id);
				let check_contract = item.check_contract
				if (contract_no != '') {
					const requestData = {
						url: '/api/order.contract_no/unique',
						showLoading: true,
						token: true,
						data: {
							contract_no: contract_no
						},
						success(res) {
							if (res.code === 200) {
								uni.showToast({
									icon: 'none',
									title: res.msg,
									duration: 3000
								});
							} else {
								uni.showToast({
									icon: 'none',
									title: res.msg,
									duration: 3000
								});
							}
						}
					};
					if (this.orderParams.news_type == 7) {
						if (check_contract == '1') {
							console.log(check_contract)
							request.request(requestData);
							return; // 跳出当前函数
						}
					} else {
						console.log(check_contract)
						request.request(requestData);
					}
				}

			}
		}
	};
</script>

<style lang="less">
	@import url("../../components/common.less");
	@import url("./common.less");
</style>
